//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbDonVi
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDonVi_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDonVi_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbDonVi OTbDonVi)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"TenDonVi", OTbDonVi.TenDonVi);
            pr[1] = new SqlParameter(@"DiaChi", OTbDonVi.DiaChi);
            pr[2] = new SqlParameter(@"Sdt", OTbDonVi.Sdt);
            pr[3] = new SqlParameter(@"Loai", OTbDonVi.Loai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDonVi_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbDonVi OTbDonVi)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"MaDonVi", OTbDonVi.MaDonVi);
            pr[1] = new SqlParameter(@"TenDonVi", OTbDonVi.TenDonVi);
            pr[2] = new SqlParameter(@"DiaChi", OTbDonVi.DiaChi);
            pr[3] = new SqlParameter(@"Sdt", OTbDonVi.Sdt);
            pr[4] = new SqlParameter(@"Loai", OTbDonVi.Loai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDonVi_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaDonVi)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDonVi",MaDonVi);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDonVi_Delete", pr);
        }
//-----------------------------------------------------------//

        public static bool KiemTraDuLieu(ETbDonVi DonVi)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDonVi", DonVi.TenDonVi);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDonVi_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static DataTable SearchDonVi(ETbDonVi DonVi)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDonVi", DonVi.TenDonVi);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDonVi_Search", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static ETbDonVi SelectByID(int MaDonVi)
        {
            ETbDonVi OTbDonVi = new ETbDonVi();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDonVi",MaDonVi);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbDonVi_SelectByID", pr);
            if (idr.Read())
              OTbDonVi = GetOneTbDonVi(idr);
            idr.Close();
            idr.Dispose();
            return OTbDonVi;
        }
//-----------------------------------------------------------//
        private static ETbDonVi GetOneTbDonVi(IDataReader idr)
       {
            ETbDonVi OTbDonVi = new ETbDonVi();
                if (idr["MaDonVi"] != DBNull.Value)
                    OTbDonVi.MaDonVi = (int)idr["MaDonVi"];
                if (idr["TenDonVi"] != DBNull.Value)
                    OTbDonVi.TenDonVi = (string)idr["TenDonVi"];
                if (idr["DiaChi"] != DBNull.Value)
                    OTbDonVi.DiaChi = (string)idr["DiaChi"];
                if (idr["Sdt"] != DBNull.Value)
                    OTbDonVi.Sdt = (string)idr["Sdt"];
                if (idr["Loai"] != DBNull.Value)
                    OTbDonVi.Loai = (int)idr["Loai"];
            return OTbDonVi;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaDonVi)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaDonVi",MaDonVi);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDonVi_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbDonVi> ListAll()
        {
            List<ETbDonVi> list = new List<ETbDonVi>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDonVi_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbDonVi(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbDonVi> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbDonVi> list = new List<ETbDonVi>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDonVi_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbDonVi(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}